package LuMin.wait_and_notify;

import java.util.concurrent.TimeUnit;

/**
 * @author lumin
 * @date 2022/04/25 19:53
 **/
public class Demo1 {
    static class MyThread extends Thread {
        private final Object o;

        MyThread(Object o) {
            this.o = o;
        }

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (o) {
                System.out.println("唤醒主线程");
                o.notify();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();


        synchronized (o) {
            MyThread t = new MyThread(o);
            t.start();
            //wait时所会被释放，不再持有锁，直到被唤醒
            o.wait();
            System.out.println("永远不会到达");
        }
    }
}